local skel = fk.CreateSkill({
  name = "lb_zero__fushu",
})

Fk:loadTranslationTable {
  ["lb_zero__fushu"] = "符术",
  [":lb_zero__fushu"] = "其他角色的判定牌生效前，你可以打出一张牌替换之，若因此反转了判定结果，回合结束时，你判定X次【闪电】（X为反转判定结果的次数）。",

  ["#lb_zero__fushu-ask"] = "符术：你可以打出一张牌替换 %dest 的“%arg”判定",

  ["@lb_zero__fushu-turn"] = "符术",

  ["$lb_zero__fushu1"] = "动静相宜，清浊相生。",
  ["$lb_zero__fushu2"] = "形神俱灭！",
}

skel:addEffect(fk.AskForRetrial, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and not player:isNude() and target ~= player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local allIds = table.connect(player:getHandlyIds(), player:getCardIds("e"))
    local ids = table.filter(allIds, function(id)
      return not player:prohibitResponse(Fk:getCardById(id))
    end)
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      pattern = tostring(Exppattern { id = ids }),
      include_equip = true,
      prompt = "#lb_zero__fushu-ask::" .. target.id .. ":" .. data.reason .. ":",
      cancelable = true,
      expand_pile = table.filter(ids, function(id)
        return not table.contains(player:getCardIds("he"), id)
      end)
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local getresult = function()
      local results = {}
      if type(data.pattern) == "table" then
        for pattern, result in pairs(data.pattern) do
          if data.card:matchPattern(pattern) then
            table.insertIfNeed(results, result)
          end
        end
        if not next(results) then
          table.insertIfNeed(results, data.pattern["else"])
        end
      else
        if data.card:matchPattern(data.pattern) then
          table.insertIfNeed(results, "good")
        else
          table.insertIfNeed(results, "bad")
        end
      end
      return table.contains(results, "bad") --失效
    end
    local result = getresult()
    player.room:changeJudge {
      card = Fk:getCardById(event:getCostData(self).cards[1]),
      player = player,
      data = data,
      skillName = skel.name,
      response = true,
      exchange = true,
    }
    if result ~= getresult() then
      player.room:addPlayerMark(player, "@lb_zero__fushu-turn")
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player:getMark("@lb_zero__fushu-turn") > 0
  end,
  on_use = function(self, event, target, player, data)
    for i = 1, player:getMark("@lb_zero__fushu-turn"), 1 do
      --闪电判定
      if player.dead then
        return
      end
      local room = player.room
      local pattern = ".|2~9|spade"
      local judge = {
        who = player,
        reason = "lightning",
        pattern = pattern,
      }
      room:judge(judge)
      if judge:matchPattern() then
        room:damage {
          to = player,
          damage = 3,
          damageType = fk.ThunderDamage,
          skillName = skel.name,
        }
      end
    end
  end,
})

return skel
